//
//  MBCommodityPreferentialAlert.swift
//  StarTimesTV
//
//  Created by 胡月明 on 2019/1/8.
//  Copyright © 2019年 StarTimes. All rights reserved.
//

import UIKit

class MBCommodityPreferentialAlert: UIView {

    var productModel: MemberProductDto? { didSet {
        guard let product = productModel else { return }
        iconImage.st_setImage(with: product.product_logo, placeholder: UIImage.init(named:"ph_star_logo"))
    } }
    
    var model: MemberCommodityDto? { didSet {
        guard let commodity = model else { return }
       titleLabel.text = commodity.name
       contentLabel.text = commodity.detail_info
        
        var height:CGFloat = 0
        let width = scale_width(280-24*2-14-4)
        let mar = scale_width(11)
        for (_,str) in commodity.promotion_note.enumerated() {
            
            let labelHeight = str.getLableHeight(font: UIFont.regular(with: 14), width: width)
            
            let view = listCell(frame: CGRect(x: 0, y: height, width: scale_width(280-24*2), height: labelHeight), content: str)
            
            height = height+labelHeight+mar
            
            listContentView.addSubview(view)
            
        }
        
        listContentView.snp.updateConstraints { (make) in
            make.height.equalTo(height)
        }
    } }
    
    func show(in view: UIView? = nil) {
        var view_ = view
        if view_ == nil {
            let appDelegate = UIApplication.shared.delegate as? AppDelegate
            view_ = appDelegate?.window
        }
        
        guard let superview = view_ else {
            close()
            return
        }
        superview.addSubview(self)
        snp.makeConstraints { make in
            make.edges.equalTo(superview)
        }
    }
    
    func listCell(frame:CGRect,content:String) -> UIView {
        
        let view = UIView.init(frame: frame)
        
        let imageView = UIImageView.init(frame: CGRect(x: 0, y: 0, width: margin14, height: margin14))
        imageView.image = UIImage.init(named:"commodity_gift")
        view.addSubview(imageView)
        
        let label = UILabel.init(frame: CGRect(x: scale_width(14+4), y: 0, width: frame.size.width-scale_width(14+4), height: frame.size.height))
        label.font = UIFont.regular(with: 14)
        label.numberOfLines = 0
        label.text = content
        view.addSubview(label)
        
        return view
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupAddSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setupLayoutSubviews()
    }
    
    private lazy var backView:UIView = {
        let view = UIView.init()
        view.backgroundColor = UIColor.black
        let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(imageViewClick(sender:)))
        view.addGestureRecognizer(singleTapGesture)
        view.alpha = 0.6
        return view
    }()
    private lazy var whitebackView:UIView = {
        let view = UIView.init()
        view.backgroundColor = UIColor.white
        return view
    }()
    
    private lazy var iconImage: UIImageView = {
        let image = UIImageView.init()
        image.contentMode = .scaleAspectFill
        return image
    }()
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel.init()
        label.numberOfLines = 1
        label.STAdjustsFontSizeToFitWidth = false
        label.font = UIFont.bold(with: fontSize16)
        label.textColor = STColor.olBlack3
        label.textAlignment = .left
        return label
    }()
    
    private lazy var lineView: UIView = {
        let view = UIView.init()
        view.backgroundColor = STColor.olSeparator
        return view
    }()
    
    private lazy var contentLabel: UILabel = {
        let view = UILabel.init()
        view.numberOfLines = 0
        view.font = UIFont.regular(with: fontSize16)
        return view
    }()
    
    private lazy var listView: UIScrollView = {
        let view = UIScrollView.init()
        return view
    }()
    
    private lazy var listContentView: UIView = {
        let view = UIView.init()
        return view
    }()
    
    private lazy var okButton: UIButton = {
        let button = UIButton.init()
        button.setTitleColor(STColor.olBlue, for:.normal)
        button.setTitle(OLLocalize("ok"), for:.normal)
        button.titleLabel?.font = UIFont.bold(with: fontSize14)
        button.addTarget(self, action:#selector(didClickOKbutton), for:.touchUpInside)
        return button
    }()
    
    func close() {
        isHidden = true
        removeFromSuperview()
    }
    
    @objc func didClickOKbutton(){
        close()
    }
    
    @objc func imageViewClick(sender:UITapGestureRecognizer){
        close()
    }
}

extension MBCommodityPreferentialAlert {
    
    private func setupAddSubviews() {
        
        addSubview(backView)
        addSubview(whitebackView)
        whitebackView.addSubview(iconImage)
        whitebackView.addSubview(lineView)
        whitebackView.addSubview(titleLabel)
        whitebackView.addSubview(okButton)
        whitebackView.addSubview(contentLabel)
        whitebackView.addSubview(listView)
        listView.addSubview(listContentView)
    }
    
    private func setupLayoutSubviews() {
        backView.snp.makeConstraints { (make) in
            make.top.left.right.bottom.equalToSuperview()
        }
        
        whitebackView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.size.equalTo(CGSize(width: scale_width(280), height: scale_width(254)))
        }
        
        iconImage.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(scale_width(21))
            make.left.equalToSuperview().offset(scale_width(24))
            make.width.equalTo(scale_width(40))
            make.height.equalTo(scale_width(40))
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(iconImage)
            make.left.equalTo(iconImage.snp.right).offset(margin20)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.top.equalTo(iconImage.snp.bottom).offset(margin13)
            make.left.equalToSuperview().offset(scale_width(24))
            make.right.equalToSuperview().offset(scale_width(-24))
            make.height.equalTo(margin2)
        }
        
        contentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(lineView.snp.bottom).offset(margin16)
            make.left.equalToSuperview().offset(scale_width(24))
            make.right.equalToSuperview().offset(scale_width(-24))
        }
        
        okButton.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(scale_width(-8))
            make.right.equalToSuperview().offset(scale_width(-8))
            make.size.equalTo(CGSize(width: scale_width(80), height: scale_width(36)))
        }
        
        listView.snp.makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp.bottom).offset(margin16)
            make.left.equalToSuperview().offset(scale_width(24))
            make.right.equalToSuperview().offset(scale_width(-24))
            make.bottom.equalTo(okButton.snp.top)
        }
        
        listContentView.snp.makeConstraints { (make) in
            make.top.left.right.bottom.equalToSuperview()
            make.size.equalTo(CGSize(width: scale_width(280-24*2), height: scale_width(200)))
        }
    }
}
